<?php
namespace app\admin\controller\manager;

use app\admin\controller\Controller;
use app\admin\model\AdminUser as AdminUserModel;

class User extends Controller
{
   protected $model;
   /**
    * 初始化
    * @access public
    */
   public function initialize()
   {
      parent::initialize();
       // 实例化Model
       $this->model = new AdminUserModel;
   }
    // 用户初始化
    public function initUser(){
       // 获取用户详细
       $userInfo = $this->userinfo;
       // 返回    
       return $this->renderSuccess($userInfo);
    }

   // 管理员列表
   public function list()
   {
      // 返回    
      return $this->renderSuccess($this->model->getAll());
   }

    // 用户详细
    public function detail()
    {
       // 返回    
       return $this->renderSuccess($this->model->detail($this->userinfo));
    }
    
   // 添加管理员
   public function add()
   {
      if($this->model->add($this->request->only(['admin_role_id', 'mobile', 'email', 'image_id', 'password', 'username'])))
      {
            return $this->renderSuccess([],'新增成功');
      }
      return $this->renderError($this->model->getError());
   }

   // 编辑管理员
   public function edit()
   {
      if($this->model->edit($this->request->only(['admin_role_id','admin_user_id', 'mobile', 'image_id', 'password', 'email', 'nickname', 'username'])))
      {
            return $this->renderSuccess([],'修改成功');
      }
      return $this->renderError($this->model->getError());
   }

   // 删除管理员
   public function delete()
   {

      if($this->model->remove($this->request->param('admin_user_id')))
      {
            return $this->renderSuccess([],'删除成功');
      }
      return $this->renderError($this->model->getError()??'删除失败');
   }

   // 编辑自己的资料
   public function editProfile()
   {
      $param = $this->request->only(['nickname', 'mobile', 'image_id', 'password', 'email']);
      $param['admin_user_id'] = $this->userinfo['admin_user_id']??0;
      $param['admin_role_id'] = $this->userinfo['adminRole']['admin_role_id']??0;
      $param['username'] = $this->userinfo['username'];
      if($this->model->edit($param))
      {
            return $this->renderSuccess([],'修改成功');
      }
      return $this->renderError($this->model->getError());
   }

}